package com.example.interfacecall.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.auth0.jwt.interfaces.JWTVerifier;
import java.security.KeyFactory;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec;
import java.util.Arrays;
import java.util.Date;
import org.springframework.util.StringUtils;
import java.util.Base64;
/**
 * @author: shuguangwang
 * @date: 2020/2/28
 * @description:
 **/
public class KeyUtils {
//    public static RSAPrivateKey getRSAPrivateKey(String privateK) throws Exception
//    {
//        byte[] clear = decryptBASE64(privateK);
//        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(clear);
//        KeyFactory fact = KeyFactory.getInstance("RSA");
//        RSAPrivateKey priv = (RSAPrivateKey) fact.generatePrivate(keySpec);
//        Arrays.fill(clear, (byte) 0);
//        return priv;
//    }
//    public static RSAPublicKey getRSAPublicKey(String publicK) throws Exception {
//        byte[] publicBytes = decryptBASE64(publicK);
//        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicBytes);
//        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
//        RSAPublicKey pubKey = (RSAPublicKey) keyFactory.generatePublic(keySpec);
//        return pubKey;
//    }
//    //解码返回 byte
//    public static byte[] decryptBASE64(String key) throws Exception {
//        return (new BASE64Decoder()).decodeBuffer(key);
//    }
//    public static RSAPublicKey defaultRSAPublicKey() throws Exception {
//        String pKey =
//                "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCXZKMjuouJMNp3jRRipLo6GSGj GuOMCJfH4/9H\n"
//                +
//                "nQpeNg5Rg8RI2J2xRI6HpNcrowNwdXSVKlYRXMStA5kFSYxGJoPL3ZMWBwp28uY0sK c0V9W3PPN5\n"
//                +
//                "GCKQbrKxsbfhBc+MmIVdHB4EWmk8Y0tzhZBYQ7/+qiYnc7sbP9l3TB0dIwIDAQAB";
//        RSAPublicKey publicKey = getRSAPublicKey(pKey);
//        return publicKey;
//    }
//    public static RSAPrivateKey defaultRSAPrivateKey() throws Exception {
//        String piKey =
//                "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAJdkoyO6i4kw2neNF GKkujoZIaMa\n"
//                +
//                "44wIl8fj/0edCl42DlGDxEjYnbFEjoek1yujA3B1dJUqVhFcxK0DmQVJjEYmg8vdkxYHCnb y5jSw\n"
//                +
//                "pzRX1bc883kYIpBusrGxt+EFz4yYhV0cHgRaaTxjS3OFkFhDv/6qJidzuxs/2XdMHR0jAg MBAAEC\n"
//                +
//                "gYBR3GPuVNdQhm8LB+kG8cWU1rP+ivKRbLANsPX/ViYeDSKu5w4CycZy7PwY0oRanx mj/I7efXbu\n"
//                +
//                "inf6iaB7YGqNSn00NicYLed2FwbR6IzQuC1DgDENzApz/Cn99jdvQPE5bhFVUJum+uXV uDpzoFZa\n"
//                +
//                "BL1AA4Roshmw9eRQS7cKAQJBANVWJIVsPSFEsQBQs15jw2TJImhr6BkyGXcwJprxyN 6/LIYkyCkg\n"
//                +
//                "24TO4Rt7FoMoN/+qkmtinEZqUhNQEvE5aQECQQC1q0aIIcYHOtqkHdpiayK4Riq6CsN cjBfSNk59\n"
//                +
//                "5urN5MLCCF2gpiQgper5ch5xbxSMPj940h7OBu0jdDs2scIjAkEAyAJ6PQjMixs+oyAF+ pGnyB72\n"
//                +
//                "flXkhXK+A0uGdjAG9ibf1Gg5rK2cgXf//ma5JYAYeD9wBPOvtijKCEdDaxs7AQJAHXG7Jsl CjV1u\n"
//                +
//                "nnbwNE9k+zT0K98etttF6R5ALovEtsZDAqvUcPs8k8YQhYmHgZ8dV3WnV+/GamIZAU wPZNf+vQJB\n"
//                +
//                "AJ3wNGKeSeNaYWorhRWrolfv6Zxz4ykvK7Y1cV08cvzN3jzv6zeu6AjqVlP/OpwHxIfaic DqWwpV\n"
//        + "f1hi+rBxXvg=";
//        RSAPrivateKey privateKey = getRSAPrivateKey(piKey);
//        return privateKey;
//    }
//    public static Boolean verify(String jwtToken) throws Exception {
//        RSAPublicKey publicKey = KeyUtils.defaultRSAPublicKey();
//        RSAPrivateKey privateKey = KeyUtils.defaultRSAPrivateKey();
//        //加密
//        Algorithm algorithm = Algorithm.RSA256(publicKey, privateKey);
//        //解密
//        JWTVerifier verifier = JWT.require(algorithm)
//                .withIssuer("cnshuguang")
//                .build();
//        DecodedJWT jwt = verifier.verify(jwtToken);
//        return !StringUtils.isEmpty(jwt);
//    }
//    public static void main(String[] args) throws Exception {
//        String name = "投保 企 业 全 称"; String mobile = "12345678901"; String
//                idNumber = "投保企业统一信用社会代码";
//        String token = encodeJwtToken(name, mobile, idNumber);
//        System.out.println(token);
//        String decodeToken = decodeJwtToken(token);
//        System.out.println(decodeToken);
//    }
//    public static String encodeJwtToken(String name, String mobile, String
//            idNumber) {
//        try {
//            RSAPublicKey publicKey = defaultRSAPublicKey();
//            RSAPrivateKey privateKey = defaultRSAPrivateKey();
//            //超时时间
//            Date expiresAt = new Date(System.currentTimeMillis() + (5 * 60 * 1000));
//            System.out.println(expiresAt.toString());
//            //加密
//            Algorithm algorithm = Algorithm.RSA256(publicKey, privateKey);
//            String token = JWT.create()
//                    .withIssuer("cnshuguang")
//                    .withClaim("name", "投保企业全称")
//                    .withClaim("mobile", "12345678901")
//                    .withClaim("idNumber", "投保企业统一信用社会代码")
//                    .withExpiresAt(expiresAt)
//                    .sign(algorithm);
//            return token;
//        } catch (Exception e){
//            e.printStackTrace();
//            return null;
//        }
//    }
//    public static String decodeJwtToken(String jwtToken) {
//        try {
//            RSAPublicKey publicKey = defaultRSAPublicKey();
//            RSAPrivateKey privateKey = defaultRSAPrivateKey();
//            //加密
//            Algorithm algorithm = Algorithm.RSA256(publicKey, privateKey);
//            //解密
//            JWTVerifier verifier = JWT.require(algorithm)
//                    .withIssuer("cnshuguang")
//                    .build(); //Reusable verifier instance
//            DecodedJWT jwt = verifier.verify(jwtToken);
//            System.out.println("........................>>>> " + jwt.toString());
//            String name = jwt.getClaim("name").asString();
//            String mobile = jwt.getClaim("mobile").asString();
//            String idNumber = jwt.getClaim("idNumber").asString();
//            String str = "name: " + name + ", mobile：" + mobile + ", idNumber：" +
//                    idNumber;
//            return str;
//        } catch (Exception e) {
//            e.printStackTrace();
//            return null;
//        }
//    }
}